<template>
  <div class="container">
    <el-card class="box-card">
      <!-- form表单 -->
      <el-form label-width="80px" :model="form">
        <!-- 第一行 -->
        <el-row type="flex" style="text-align:right">
          <el-col :span="24">
            <el-button type="success" icon="el-icon-edit" @click="$router.push('/questions/new')">新增试题</el-button>
          </el-col>
        </el-row>
        <!-- 第二行 -->
        <el-row type="flex" style="margin-top:20px">
          <el-col :span="6">
            <el-form-item label="学科">
              <el-select v-model="form.subjectID" placeholder="请选择" @change="changeSub">
                <el-option v-for="item in subList" :key="item.id" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="二级目录">
              <el-select v-model="form.catalogID" placeholder="请选择">
                <el-option v-for="item in contents" :key="item.id" :label="item.directoryName" :value="item.directoryName" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6">
            <el-form-item label="标签">
              <el-select v-model="form.tags" placeholder="请选择">
                <el-option v-for="item in tagsList" :key="item.value" :label="item.label" :value="item.label" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input v-model="form.keyword" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 第三行 -->
        <el-row type="flex" style="margin-top:20px">
          <el-col :span="6">
            <el-form-item label="试题类型">
              <el-select v-model="form.questionType" placeholder="请选择">
                <el-option v-for="item in testType" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="难度">
              <el-select v-model="form.difficulty" placeholder="请选择">
                <el-option v-for="item in hardList" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向">
              <el-select v-model="form.direction" placeholder="请选择">
                <el-option v-for="(item,index) in direction" :key="index" :label="item" :value="item" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人">
              <el-select v-model="form.people" placeholder="请选择" class="enrollPeople">
                <el-option v-for="item in usersList" :key="item.id" :label="item.username" :value="item.username" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 第四行 -->
        <el-row type="flex" style="margin-top:20px">
          <el-col :span="6">
            <el-form-item label="题目备注">
              <el-input v-model="form.remarks" class="remark" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称">
              <el-input v-model="form.companyName" class="remark" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-row>
              <el-form-item label="城市">
                <el-col :span="9">
                  <el-select v-model="form.province" placeholder="请选择" @change="selectAreaFn">
                    <el-option v-for="(item,index) in cityList" :key="index" :label="item" :value="item" />

                  </el-select>
                </el-col>
                <el-col :span="9">
                  <el-select v-model="form.city" placeholder="请选择">
                    <el-option v-for="(item,index) in area" :key="index" :label="item" :value="item" />
                  </el-select>
                </el-col>
              </el-form-item>
            </el-row>
          </el-col>

          <el-col :span="6">
            <el-row :gutter="20">
              <el-col :span="6"><div class="grid-content bg-purple" /></el-col>
              <el-col :span="6"><div class="grid-content bg-purple" /></el-col>
              <el-col :span="6">
                <el-button @click="clearFn">清除</el-button>
              </el-col>
              <el-col :span="6">
                <el-button type="primary" @click="searchFn">搜索</el-button>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-form>

      <!-- 提示框 -->
      <el-alert
        :title="'数据一共'+total+'条'"
        type="info"
        show-icon
        :closable="false"
      />

      <!-- 表格 -->
      <el-table
        class="tableBox"
        :data="subjectData"
        style="width: 100%"
      >
        <el-table-column
          prop="number"
          label="试题编号"
          width="180"
        />
        <el-table-column
          prop="subject"
          label="学科"
          width="100"
        />
        <el-table-column
          prop="catalog"
          label="目录"
        />
        <el-table-column
          prop="questionType"
          label="题型"
          :formatter="formatterTwo"
        />
        <el-table-column
          label="题干"
        >
          <template slot-scope="scope">
            <div v-html="scope.row.question" />
          </template>
        </el-table-column>
        <!-- prop="addDate" -->
        <el-table-column
          label="录入时间"
        >
          <template slot-scope="scope">
            {{ timeFormat(scope.row.addDate) }}
          </template>
        </el-table-column>

        <el-table-column
          prop="difficulty"
          label="难度"
          width="100"
          :formatter="formatter"
        />
        <el-table-column
          prop="creator"
          label="录入人"
        />
        <el-table-column
          label="操作"
          width="180"
        >
          <template slot-scope="scope">
            <!-- 查看 -->
            <el-button type="primary" icon="el-icon-view" size="mini" circle plain @click="lookFn(scope)" />
            <!-- 修改 -->
            <el-button type="success" icon="el-icon-edit" size="mini" circle plain @click="editFn(scope)" />
            <!-- 删除 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" circle plain @click="delFn(scope)" />
            <!-- 对勾 -->
            <el-button type="warning" icon="el-icon-check" size="mini" circle plain @click="QuestionBankFn(scope)" />
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页  -->
      <el-pagination
        class="pageBox"
        background
        :current-page="query.page"
        :page-sizes="[2, 3, 5, 20]"
        :page-size="query.pagesize"
        layout="sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />

      <el-dialog
        title="题目预览"
        :visible.sync="showDialog"
        width="50%"
      >
        <questions-preview :son-date="sonDate" :button-status="buttonStatus" @closeBox="closeFn" />
      </el-dialog>

    </el-card>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { list as userList } from '@/api/base/users.js'
import { list, subjectList, remove, choiceAdd, detail } from '@/api/hmmm/questions.js'
// 目录列表
import { list as mulu } from '@/api/hmmm/directorys.js'
// 标签列表
import { simple } from '@/api/hmmm/tags.js'

// 导入枚举数据
import { provinces, citys } from '@/api/hmmm/citys.js'
import { difficulty, questionType, direction } from '@/api/hmmm/constants.js'
// 导入子组件
import QuestionsPreview from '@/module-hmmm/components/questions-preview'
export default {
  name: 'Questions',
  components: {
    QuestionsPreview
  },
  data() {
    return {
      query: {
        page: 1, // 页码
        pagesize: 2 // 每页请求的条数
      },

      form: {
        subjectID: '', // 学科
        catalogID: '', // 二级目录
        tags: '', // 标签
        questionType: '', // 试题类型
        difficulty: '', // 难度
        direction: '', // 方向
        remarks: '', // 题目备注
        enterpriseID: '', // 企业
        province: '', // 城市
        city: '', // 地区
        people: '', // 录入人
        keyword: '', // 关键字
        companyName: '' // 企业简称

      },
      tagsList: [], // 标签列表
      contents: [], // 目录列表
      hardList: difficulty, // 难度
      testType: questionType, // 试题类型
      usersList: [], // 用户列表
      direction: direction, // 方向
      cityList: provinces(),
      area: [], // 区列表
      subList: [], // 学科列表
      subjectData: [], // 表格数据
      total: 0, // 总条目数
      showDialog: false, // 弹框的可见性
      sonDate: {}, // 子组件数据
      buttonStatus: '' // 试题类型
    }
  },
  created() {
    // 调用获取基础题库列表数据方法
    this.getQuestionsList()
    // 调用获取学科列表的方法
    this.subjectList()
    // 调用获取用户列表方法
    this.userList()
  },
  methods: {
    timeFormat(time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },
    async searchFn() {
      const res = await list({ subjectID: this.form.subjectID })
      this.subjectData = res.data.items
      // 让总数变为初始值
      this.total = 0
      // 重新给总条数赋值
      this.total = res.data.counts
    },
    async lookFn(scope) {
      this.showDialog = true
      const res = await detail({ id: scope.row.id })
      // console.log(res)
      this.sonDate = res.data
      this.buttonStatus = scope.row.questionType
    },
    // 关闭子组件弹窗
    closeFn() {
      this.showDialog = false
    },
    // 加入精选题库
    async QuestionBankFn(scope) {
      const confirmRes = await this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).catch(err => err)
      if (confirmRes === 'cancel') return this.$message('您已取消加入精选')
      await choiceAdd({ id: scope.row.id, choiceState: 1 })
      // 调用获取学科列表的方法
      this.getQuestionsList()
      this.$message.success('加入精选题库成功')
      this.$router.push('/questions/choice')
    },
    // 修改功能
    editFn(scope) {
      // console.log(scope)
      this.$router.push(`/questions/new?user=${scope.row.id}`)
    },
    // 清除按钮
    clearFn() {
      this.form = {
        subjectID: '',
        catalogID: '',
        tags: '',
        questionType: '',
        difficulty: '',
        direction: '',
        remarks: '',
        enterpriseID: '',
        province: '',
        city: '',
        people: '',
        keyword: '',
        companyName: ''

      }
    },
    // 改变学科列表时
    async changeSub() {
      // console.log(this.form.subjectID)
      const res = await mulu({ subjectID: this.form.subjectID })
      this.contents = res.data.items
      const resp = await simple({ subjectID: this.form.subjectID })
      this.tagsList = resp.data
    },
    // 获取用户列表
    async userList() {
      const res = await userList()
      // console.log(res)
      this.usersList = res.data.list
    },
    // 选择地区
    selectAreaFn() {
      this.area = citys(this.form.province)
    },
    async delFn(scope) {
      // console.log(scope.row)
      // 消息确认框
      const confirmRes = await this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes === 'cancel') return this.$message('你已取消删除')
      // 调用删除的接口
      await remove(scope.row)
      // 如果列表为1条数据时
      if (this.subjectData.length === 1) {
        this.query.page--
        // 如果我们删除的是第一页的最后一条数据，需要将 page 重置为 1
        if (this.query.page <= 0) {
          this.query.page = 1
        }
      }
      this.$message.success('删除成功')
      // 调用获取基础题库列表数据方法
      this.getQuestionsList()
    },
    // 学科列表
    async subjectList() {
      const res = await subjectList({ subjectName: this.form.subjectID })

      this.subList = res.data
    },

    // 获取基础题库列表
    async getQuestionsList() {
      const res = await list(this.query)
      console.log(res)
      this.total = res.data.counts
      // this.query.page = res.data.page
      this.subjectData = res.data.items
    },
    // 每页显示的条数发生变化时触发
    handleSizeChange(newSize) {
      this.query.pagesize = newSize
      this.getQuestionsList()
    },

    // 页码发生变化时触发
    handleCurrentChange(newPage) {
      this.query.page = newPage
      this.getQuestionsList()
    },
    formatter(row, column, cellValue, index) {
      const obj = difficulty.find(item => item.value === Number(cellValue))
      return obj ? obj.label : '未知'
    },
    formatterTwo(row, column, cellValue, index) {
      const obj = questionType.find(item => item.value === Number(cellValue))
      return obj ? obj.label : '未知'
    }

  }
}
</script>

<style scoped lang='scss'>
  .el-row {
    margin-bottom: 20px;
    &:last-child {
      margin-bottom: 0;
    }
  }
  .el-col {
    border-radius: 4px;
  }
  .bg-purple-dark {
    background: #fff;
  }
  .bg-purple {
    background: #fff;
  }
  .bg-purple-light {
    background: #fff;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
  .tableBox{
    margin-top: 15px;
  }

  .pageBox{
    text-align: right;
    margin-top: 10px;
  }
  .remark{
    width: 218px;
  }
  .enrollPeople{
    width: 100%;
  }
.container{
  padding: 10px;
}
</style>
